/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bakenote.components;

import org.apache.pivot.wtk.ImageView;
import org.apache.pivot.wtk.media.Picture;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author lello
 */
public class RateImageView extends ImageView {

	private int trate = 0;
	private int rate = 0;

	public RateImageView() { }

	public void chooseRate(int x, int y) {

		trate = getRate(x, y);
		showRate(trate);

	}


	public void setRate(int x, int y) {

		rate = trate;
		setRate(rate);

	}

	public void setRate(int rate) {

		this.rate = rate;

		switch (rate) {
			case 0: 
				setImage(Rating.RATE0.getImage());
				break;
			case 1: 
				setImage(Rating.RATE1.getImage());
				break;
			case 2: 
				setImage(Rating.RATE2.getImage());
				break;
			case 3: 
				setImage(Rating.RATE3.getImage());
				break;
			case 4: 
				setImage(Rating.RATE4.getImage());
				break;
			case 5: 
				setImage(Rating.RATE5.getImage());
				break;
		}

	}



	public void setChosenRate() {
		setRate(rate);
	}

	public void showRate(int trate) {

		switch (trate) {
			case 0:
				setImage(Rating.RATE0.getImage());
				break;
			case 1:
				setImage(Rating.RATE1o.getImage());
				break;
			case 2:
				setImage(Rating.RATE2o.getImage());
				break;
			case 3:
				setImage(Rating.RATE3o.getImage());
				break;
			case 4:
				setImage(Rating.RATE4o.getImage());
				break;
			case 5:
				setImage(Rating.RATE5o.getImage());
				break;
		}

	}


	public int getRate() {
		return rate;
	}


	private int getRate(int x, int y) {

		int r = 0;

		//System.out.println("rate at "+x);
		if (x > 1 && x < 17) {
		//System.out.println("rate 1");
			r = 1;
		} else if (x > 19 && x < 39) {
		//System.out.println("rate 2");
			r = 2;
		} else if (x > 39 && x < 60) {
		//System.out.println("rate 3");
			r = 3;
		} else if (x > 60 && x < 80) {
		//System.out.println("rate 4");
			r = 4;
		} else if (x > 80 && x < 100) {
		//System.out.println("rate 5");
			r = 5;
		}

		return r;

	}


	private enum Rating {

		RATE0("rate0.png"),
		RATE1o("rate1o.png"),
		RATE2o("rate2o.png"),
		RATE3o("rate3o.png"),
		RATE4o("rate4o.png"),
		RATE5o("rate5o.png"),
		RATE1("rate1.png"),
		RATE2("rate2.png"),
		RATE3("rate3.png"),
		RATE4("rate4.png"),
		RATE5("rate5.png");

		private BufferedImage bimg = null;

		Rating(String fileName) {
			try {
				bimg = ImageIO.read(getClass().getResource("icons" + File.separator + fileName));
			} catch (IOException ex) {
				bimg = null;
				Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
			}
		}

		public Picture getImage() {
			return new Picture(bimg);
		}
	}

}

